package com.tfjybj.itoo.teach.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.entity.YearSemesterEntity;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.CourseEntity;
import com.tfjybj.itoo.teach.entity.TrainingProgramsEntity;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.model.ProfessionModel;
import com.tfjybj.itoo.teach.model.TrainingProgramsModel;
import com.tfjybj.itoo.teach.provider.dao.TrainingProgramsDao;
import com.tfjybj.itoo.teach.provider.service.AcademyService;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import com.tfjybj.itoo.teach.provider.service.ProfessionService;
import com.tfjybj.itoo.teach.provider.service.TrainingProgramsService;
import com.tfjybj.itoo.teach.template.TrainingProgramsExportTemplate;
import com.tfjybj.itoo.teach.template.TrainingProgramsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.jplus.hyberbin.excel.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;

import static com.tfjybj.itoo.teach.provider.until.DemoConstants.*;

/**
 * TrainingProgramsService接口实现类
 * ${base}表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Service
@Slf4j
public class TrainingProgramsServiceImpl extends BaseServicePlusImpl<TrainingProgramsDao, TrainingProgramsEntity> implements TrainingProgramsService {

    @Resource
    private CourseService courseService;

    @Resource
    private AcademyService academyService;

    @Resource
    private ProfessionService professionService;

    @Resource
    private RedisTemplate<String, TrainingProgramsTemplate> redisTemplate;
    //region 模板生成
    @Resource
    private TrainingProgramsDao trainingProgramsDao;

    //下载导入EXCEL模板需要的列
    private static final String[] DOWNLOAD_IMPORT_COLUMNS = new String[]{"semesterName", "professionName", "grade", "courseCode", "courseName", "theoryClassHour", "practiceClassHour", "examinationForm"};

    //导出EXCEL模板需要的列
    private static final String[] EXPORT_COLUMNS = new String[]{"semesterName", "professionName", "grade", "courseCode", "courseName", "courseType", "courseCategory", "theoryClassHour", "practiceClassHour", "classHour", "examinationForm"};

    @Override
    public PageInfo<TrainingProgramsEntity> queryByCourseId(String courseId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(trainingProgramsDao.queryByCourseId(courseId));
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据课程类型ID查询课程信息
     *
     * @param courseTypeId 课程类型id
     * @return 课程信息实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @Override
    public List<TrainingProgramsModel> queryCourseByCourseTypeId(String courseTypeId) {
        return trainingProgramsDao.queryCourseByCourseTypeId(courseTypeId);
    }

    /**
     * 根据条件模糊分页查询培养计划--培养计划专用
     *
     * @param academyId    学院ID
     * @param professionId 专业ID
     * @param grade        年级
     * @param pageNo       页码
     * @param pageSize     每页条数
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-16 10:11:53
     */
    @Override
    public PageInfo<TrainingProgramsModel> queryTrainingProgramByProIdListAndGradList(String academyId, String professionId, String grade, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);

        List<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsDao.queryTrainingProgramByAcPrIdAndGrade(academyId, professionId, grade);

        return new PageInfo<>(importValue(trainingProgramsModelList));
    }

    /**
     * 根据课程ID查询专业id，专业名称
     *
     * @param courseId 课程id
     * @return 课程信息实体
     * @author 李建帅
     * @since 2.0.0 2018-11-10 21:34:17
     */
    @Override
    public List<TrainingProgramsModel> queryProfessionByCourseId(String courseId) {
        return trainingProgramsDao.queryProfessionByCourseId(courseId);
    }

    /**
     * 根据条件模糊分页查询培养计划
     *
     * @param strLike  模糊条件（课程名称）
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-11 16:27:44
     */
    @Override
    public PageInfo<TrainingProgramsModel> queryTrainingProgramListPageInfo(String strLike, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(importValue(trainingProgramsDao.queryTrainingProgramListPageInfo(strLike.trim())));
    }

    /**
     * 分页查询所有TrainingPrograms
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 李建帅
     * @since 2.0.0 2018-11-12 14:52:50
     */
    @Override
    public PageInfo<TrainingProgramsModel> queryAllTrainingProgramsPageInfo(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(importValue(trainingProgramsDao.queryAllTrainingProgramsPageInfo()));
    }

    /**
     * 查询所有年级
     *
     * @return 培养计划实体-年级集合
     * @author 李建帅
     * @since 2.0.0 2018/11/13 19:34
     */
    @Override
    public List<YearSemesterEntity> queryAllGrade() {
        return YearSemester.getYearList(-3, 3);
    }

    /**
     * 根据课程ID、专业ID、学期学年ID查询培养计划
     *
     * @param courseId     课程ID
     * @param professionId 专业ID
     * @param semesterId   学年学期ID
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/13 20:14
     */
    @Override
    public List<TrainingProgramsModel> queryTpByCoPrSeId(String courseId, String professionId, String semesterId) {
        return trainingProgramsDao.queryTpByCoPrSeId(courseId, professionId, semesterId);
    }

    /**
     * 根据培养计划ID查询培养计划详情
     *
     * @param idList 培养计划ID
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-15 17:17:03
     */
    @Override
    public List<TrainingProgramsModel> queryTrainingProgramsById(List<String> idList) {
        return importValue(trainingProgramsDao.queryTrainingProgramsById(idList));
    }

    /**
     * 根据学年学期ID和专业ID查询课程信息
     *
     * @param semesterId   学年学期ID
     * @param professionId 专业ID
     * @return 课程信息
     * @author 李建帅
     * @since 2.0.0 2018/11/17 9:09
     */
    @Override
    public List<TrainingProgramsModel> queryCourseBySemProId(String semesterId, String professionId) {
        return trainingProgramsDao.queryCourseBySemProId(semesterId, professionId);
    }

    /**
     * 根据学年学期ID和专业ID,年级（课程类型为专选课已写死）查询专选课课程ID集合 ---选课使用
     *
     * @param professionId 专业ID
     * @param grade        年级
     * @return 公选课课程ID集合
     * @author 李建帅
     * @since 2.0.0 2018/11/17 19:18
     */
    @Override
    public List<String> queryCourseIdBySemProId(String professionId, String grade) {
        String schoolyear = YearSemester.getYearSemesterCode(1, 1).get(0);
        return trainingProgramsDao.queryCourseIdBySemProId(schoolyear, professionId, grade);
    }

    /**
     * 根据学年学期ID、专业ID、课程ID、年级查询培养计划是否已存在
     *
     * @param semesterId   学年学期ID
     * @param professionId 专业ID
     * @param courseId     课程ID
     * @param grade        年级
     * @return 培养计划ID
     * @author 李建帅
     * @since 2.0.0 2018/11/17 19:19
     */
    @Override
    public String findIdIsExist(String semesterId, String professionId, String courseId, String grade) {
        return trainingProgramsDao.findIdIsExist(semesterId, professionId, courseId, grade);
    }

    /**
     * 下载培养计划模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 李建帅
     * @since 2.0.0 2018-11-15 08:53:53
     */
    @Override
    public boolean downLoadTemplate(HttpServletResponse response) {
        Map<Serializable, Serializable> map = new HashMap<>(16);
        List<TrainingProgramsTemplate> templateList = new ArrayList<>();
        TrainingProgramsTemplate trainingProgramsTemplate = new TrainingProgramsTemplate();
        trainingProgramsTemplate.setProfessionName("数学与应用数学");
        trainingProgramsTemplate.setSemesterName("2017-2018学年第二学期");
        trainingProgramsTemplate.setGrade("2015");
        trainingProgramsTemplate.setCourseCode("041100002");
        trainingProgramsTemplate.setCourseName("随机应用");
        trainingProgramsTemplate.setTheoryClassHour("10");
        trainingProgramsTemplate.setPracticeClassHour("10");
        trainingProgramsTemplate.setExaminationForm("考查");

        templateList.add(trainingProgramsTemplate);

        //sheet的名字
        map.put(SHEET_NAME, "培养计划");
        //需要导入的字段
        map.put(COLUMNS, DOWNLOAD_IMPORT_COLUMNS);
        //表格的标题
        map.put(TITLE, "培养计划");
        map.put(NAME, "培养计划模板");
        map.put(DATA_LIST, (Serializable) templateList);

        try {
            ExcelUtil.exportExcel(map, response);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据培养计划ID导出培养计划
     *
     * @param response response
     * @param idList   培养计划Id
     * @return 导出的结果
     * @author 李建帅
     * @since 2.0.0 2018-11-18 09:09:23
     */
    @Override
    public ItooResult exportByTpId(HttpServletResponse response, List<String> idList) {
        Map<Serializable, Serializable> map = new HashMap<>(16);

        map.put(SHEET_NAME, "培养计划");
        //要导出的字段
        map.put(COLUMNS, EXPORT_COLUMNS);
        //导出表格的标题
        map.put(TITLE, "培养计划信息");
        map.put(NAME, "培养计划信息表");
        //要导出的数据
        List<TrainingProgramsExportTemplate> trainingProgramsExportTemplateList = new ArrayList<>();

        List<TrainingProgramsModel> trainingProgramsModelList = importValue(trainingProgramsDao.queryTrainingProgramsById(idList));
        for (TrainingProgramsModel trainingProgramsModel : trainingProgramsModelList) {
            TrainingProgramsExportTemplate trainingProgramsExportTemplate = new TrainingProgramsExportTemplate();
            BeanUtils.copyProperties(trainingProgramsModel, trainingProgramsExportTemplate);
            trainingProgramsExportTemplateList.add(trainingProgramsExportTemplate);
        }
        //为空不进行导出
        if (CollectionUtils.isEmpty(trainingProgramsExportTemplateList)) {
            return ItooResult.build(ItooResult.FAIL, "没有要导出的数据！");
        }
        map.put(DATA_LIST, (Serializable) trainingProgramsExportTemplateList);

        //导出
        try {
            ExcelUtil.exportExcel(map, response);
        } catch (Exception e) {
            log.error("导出失败,未知的异常--" + e);
            return ItooResult.build(ItooResult.FAIL, "导出信息失败！", e);
        }
        return ItooResult.build(ItooResult.SUCCESS, "导出信息成功！");
    }

    /**
     * 根据条件导出培养计划
     *
     * @param response response
     * @param strLike  培养计划Id
     * @return 导出的结果
     * @author 李建帅
     * @since 2.0.0 2018-11-18 09:09:23
     */
    @Override
    public ItooResult exportByStrLike(HttpServletResponse response, String academyId, String professionId, String grade, String strLike) {
        Map<Serializable, Serializable> map = new HashMap<>(16);

        map.put(SHEET_NAME, "培养计划");
        //要导出的字段
        map.put(COLUMNS, EXPORT_COLUMNS);
        //导出表格的标题
        map.put(TITLE, "培养计划信息");
        map.put(NAME, "培养计划信息表");
        //要导出的数据
        List<TrainingProgramsExportTemplate> trainingProgramsExportTemplateList = new ArrayList<>();

        List<TrainingProgramsModel> trainingProgramsModelList = importValue(trainingProgramsDao.exportByStrLike(academyId, professionId, grade, strLike));
        for (TrainingProgramsModel trainingProgramsModel : trainingProgramsModelList) {
            TrainingProgramsExportTemplate trainingProgramsExportTemplate = new TrainingProgramsExportTemplate();
            BeanUtils.copyProperties(trainingProgramsModel, trainingProgramsExportTemplate);
            trainingProgramsExportTemplateList.add(trainingProgramsExportTemplate);
        }
        //为空不进行导出
        if (CollectionUtils.isEmpty(trainingProgramsExportTemplateList)) {
            return ItooResult.build(ItooResult.FAIL, "没有要导出的数据！");
        }
        map.put(DATA_LIST, (Serializable) trainingProgramsExportTemplateList);

        //导出
        try {
            ExcelUtil.exportExcel(map, response);
        } catch (Exception e) {
            log.error("导出失败,未知的异常--" + e);
            return ItooResult.build(ItooResult.FAIL, "导出信息失败！", e);
        }
        return ItooResult.build(ItooResult.SUCCESS, "导出信息成功！");
    }

    /**
     * 批量导入
     *
     * @param
     * @return
     * @author 李建帅
     * @since 2.0.0 2018/11/15 14:29
     */
    @Override
    public ItooResult importTemplate(MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("UTF-8");

        try {
            //校验文件是否存在
            String fileName = multipartFile.getOriginalFilename();
            if (fileName == null) {
                log.error("导入失败,fileName is null!");
                return ItooResult.build(ItooResult.FAIL, "传入的文件为空！");
            }

            //EXCEL解析成List
            Map<Serializable, Serializable> map = new HashMap<>(4);
            map.put(SHEET_NAME, "培养计划");
            map.put(CLASS, TrainingProgramsTemplate.class);
            List<TrainingProgramsTemplate> trainingProgramsTemplateList = ExcelUtil.importExcel(Objects.requireNonNull(fileName), map, request, response);

            if (CollectionUtils.isEmpty(trainingProgramsTemplateList)) {
                return ItooResult.build(ItooResult.FAIL, "导入的数据为空，请填写！");
            }
            //定义导入的错误数据集合并存redis中形式的<String,TrainingProgramsTemplate>
            List<TrainingProgramsTemplate> errorTPTList = new ArrayList<>();
            String errorTPTListId = IdWorker.getIdStr();
            Long size = redisTemplate.opsForZSet().size(errorTPTListId);
            size = size == null ? 0L : size;

            for (TrainingProgramsTemplate excelTPT : trainingProgramsTemplateList) {

                if (!this.verify(excelTPT)) {
                    TrainingProgramsTemplate trainingProgramsT = new TrainingProgramsTemplate();
                    BeanUtils.copyProperties(excelTPT, trainingProgramsT);

                    errorTPTList.add(trainingProgramsT);
                    redisTemplate.opsForZSet().add(errorTPTListId, trainingProgramsT, size + 1);
                    continue;
                }

                TrainingProgramsEntity trainingProgramsEntity = insertValue(excelTPT);

                this.save(trainingProgramsEntity);
            }
            //不符合条件的信息返回
            if (!CollectionUtils.isEmpty(errorTPTList)) {
                return ItooResult.build("0001", "部分导入成功！", errorTPTListId);
            }
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "导入数据异常！", e);
        }
        return ItooResult.build(ItooResult.FAIL, "导入数据成功！");
    }

    /**
     * 将模板的值插入实体中-导入专用
     *
     * @param trainingProgramsTemplate 导入的模板
     * @return trainingProgramsEntity 培养计划实体
     * @author 李建帅
     * @since 2.0.0 2018/12/2 10:44
    */
    private TrainingProgramsEntity insertValue(TrainingProgramsTemplate trainingProgramsTemplate){
        TrainingProgramsEntity trainingProgramsEntity = new TrainingProgramsEntity();
        //将导入内容放到entity中对应的属性中去
        BeanUtils.copyProperties(trainingProgramsTemplate, trainingProgramsEntity);

        //培养计划ID
        trainingProgramsEntity.setId(IdWorker.getIdStr());
        //可用
        trainingProgramsEntity.setIsUse(1);
        //未排课
        trainingProgramsEntity.setIsArrangement(0);

        //考核形式-需要转化成ID
        trainingProgramsEntity.setExaminationFormId(getIdByExaminationForm(trainingProgramsTemplate.getExaminationForm().trim()));

        //选填数据
        //操作者
        trainingProgramsEntity.setOperator("批量导入");
        //备注
        trainingProgramsEntity.setRemark("开发测试数据");
        //单双周
        trainingProgramsEntity.setOddWeekOrNot(0);

        return trainingProgramsEntity;
    }
    /**
     * 根据考核形式名称获得ID
     *
     * @param examinationForm 考核形式名称
     * @return 考核形式ID
     * @author 李建帅
     * @since 2.0.0 2018/11/17 15:06
     */
    private String getIdByExaminationForm(String examinationForm) {
        Map<String, String> examinationFormMap = new HashMap<>();
        examinationFormMap.put("考查", "1");
        examinationFormMap.put("考试", "2");

        return examinationFormMap.get(examinationForm);
    }

    /**
     * 判断导入的信息是否为空
     *
     * @param excelTPT 需要导入的信息
     * @return boolean 导入的字段是否为空
     * @author 李建帅
     * @since 2.0.0 2018/11/16 20:43
     */
    private boolean verify(TrainingProgramsTemplate excelTPT) {
        if (StringUtils.isEmpty(excelTPT.getCourseCode())) {
            excelTPT.setFailReason("课程代码未填写");
            log.warn("导入失败,课程代码未填写,excelFooEntity-{}", excelTPT);
            return false;
        }
        if (StringUtils.isEmpty(excelTPT.getCourseName())) {
            excelTPT.setFailReason("课程名称未填写");
            log.warn("导入失败,课程名称未填写,excelFooEntity-{}", excelTPT);
            return false;
        }
        if (StringUtils.isEmpty(excelTPT.getGrade())) {
            excelTPT.setFailReason("年级未填写");
            log.warn("导入失败,年级未填写,excelFooEntity-{}", excelTPT);
            return false;
        }
        if (StringUtils.isEmpty(excelTPT.getProfessionName())) {
            excelTPT.setFailReason("专业名称未填写");
            log.warn("导入失败,专业名称未填写,excelFooEntity-{}", excelTPT);
            return false;
        }
        if (StringUtils.isEmpty(excelTPT.getSemesterName())) {
            excelTPT.setFailReason("学年学期未填写");
            log.warn("导入失败,学年学期未填写,excelFooEntity-{}", excelTPT);
            return false;
        }

        //通过专业名称获取专业数据
        List<ProfessionModel> professionModels = professionService.queryProfessionsByName(excelTPT.getProfessionName().trim());
        //判断是否存在该专业，不存在记录下错误原因
        if (CollectionUtils.isEmpty(professionModels)) {
            excelTPT.setFailReason("找不到该专业名称");
            log.warn("找不到专业名称-",excelTPT);
            return false;
        }
        //存在即将专业ID存入
        excelTPT.setProfessionId(professionModels.get(0).getId());

        //通过课程ID查询课程的信息
        List<CourseModel> courseModel = courseService.isExistByCode(excelTPT.getCourseCode());
        if (CollectionUtils.isEmpty(courseModel)) {
            excelTPT.setFailReason("找不到课程代码为:" + excelTPT.getCourseCode() + "的课程");
            log.warn("找不到课程代码为:" + excelTPT.getCourseCode() + "的课程");
            return false;
        }

        //判断查询出的课程名称与导入时写入的课程名称是否相同
        if (!courseModel.get(0).getName().equals(excelTPT.getCourseName().trim())) {
            excelTPT.setFailReason("课程代码：" + excelTPT.getCourseCode() + "所对应的课程名称与输入的课程名称：" + excelTPT.getCourseName() + "不匹配");
            log.warn("课程代码：" + excelTPT.getCourseCode() + "所对应的课程名称与输入的课程名称：" + excelTPT.getCourseName() + "不匹配-",excelTPT);
            return false;
        }

        //判断课程类型时候是否为公选课类型，为公选课则返回错误信息，不导入
        if (!StringUtils.isEmpty(courseModel.get(0).getCourseType())) {
            if (courseModel.get(0).getCourseType().equals("4")) {
                excelTPT.setFailReason("不支持导入公共选修课课程类型");
                log.warn("不支持导入公共选修课课程类型-", excelTPT);
                return false;
            }
        }

        //课程信息无误即存入课程信息
        //课程ID
        excelTPT.setCourseId(courseModel.get(0).getId());

        //课程类型ID
        excelTPT.setCourseTypeId(courseModel.get(0).getCourseType());

        //课程分数 -默认100分
        excelTPT.setCourseScore(Double.parseDouble(StringUtils.isEmpty(courseModel.get(0).getScore()) ? "100" : courseModel.get(0).getScore()));

        //课程类别ID
        excelTPT.setCourseCategoryId(courseModel.get(0).getCourseCategory());

        //学年ID
        excelTPT.setSemesterId(YearSemester.getYearSemesterCodeByName(excelTPT.getSemesterName().trim()));

        //判断培养计划是否已经存在
        String idIsExist = trainingProgramsDao.findIdIsExist(excelTPT.getSemesterId(), excelTPT.getProfessionId(), excelTPT.getCourseId(), excelTPT.getGrade().trim());
        if (!StringUtils.isEmpty(idIsExist)) {
            excelTPT.setFailReason("专业：" + excelTPT.getProfessionName() + "年级：" + excelTPT.getGrade() + "学期：" + excelTPT.getSemesterName() + "课程：" + excelTPT.getCourseName() + "已存在对应的培养计划，不能重复添加");
            log.warn("专业：" + excelTPT.getProfessionName() + "年级：" + excelTPT.getGrade() + "学期：" + excelTPT.getSemesterName() + "课程：" + excelTPT.getCourseName() + "已存在对应的培养计划，不能重复添加-",excelTPT);
            return false;
        }

        return true;
    }

    /**
     * 将培养计划中的名称字段转换为ID并填充到实体中去
     *
     * @param trainingProgramsModelList 培养计划实体集合
     * @return 填充好数据的培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/17 14:21
     */
    private List<TrainingProgramsModel> importValue(List<TrainingProgramsModel> trainingProgramsModelList) {
        if (CollectionUtils.isEmpty(trainingProgramsModelList)) {
            return trainingProgramsModelList;
        }

        //将类型ID转换成具体的名称
        //课程类型（1-专业必修，2-公共必修，3-专业选修，4-公共选修）
        Map<String, String> courseTypeMap = new HashMap<>();
        courseTypeMap.put("1", "专业必修");
        courseTypeMap.put("2", "公共必修");
        courseTypeMap.put("3", "专业选修");
        courseTypeMap.put("4", "公共选修");

        //课程类别（1-理论课、2-实践课、3-理论实践课）；
        Map<String, String> courseCategoryMap = new HashMap<>();
        courseCategoryMap.put("1", "理论课");
        courseCategoryMap.put("2", "实践课");
        courseCategoryMap.put("3", "理论实践课");

        //考核形式（1-考查、2-考试）
        Map<String, String> examinationFormMap = new HashMap<>();
        examinationFormMap.put("1", "考查");
        examinationFormMap.put("2", "考试");

        //填充数据
        for (int i = 0; i < trainingProgramsModelList.size(); i++) {

            //补充学年学期名称
            if (!StringUtils.isEmpty(trainingProgramsModelList.get(i).getSemesterId())) {
                trainingProgramsModelList.get(i).setSemesterName(YearSemester.getYearSemesterNameByCode(trainingProgramsModelList.get(i).getSemesterId()));
            }

            //填充课程类型
            if (!StringUtils.isEmpty(trainingProgramsModelList.get(i).getCourseTypeId())) {
                String courseTypeId = trainingProgramsModelList.get(i).getCourseTypeId();
                trainingProgramsModelList.get(i).setCourseType(courseTypeMap.get(courseTypeId));
            }

            //填充课程类别
            if (!StringUtils.isEmpty(trainingProgramsModelList.get(i).getCourseCategoryId())) {
                String courseCategoryId = trainingProgramsModelList.get(i).getCourseCategoryId();
                trainingProgramsModelList.get(i).setCourseCategory(courseCategoryMap.get(courseCategoryId));
            }

            //填充考核形式
            if (!StringUtils.isEmpty(trainingProgramsModelList.get(i).getExaminationFormId())) {
                String examinationFormId = trainingProgramsModelList.get(i).getExaminationFormId();
                trainingProgramsModelList.get(i).setExaminationForm(examinationFormMap.get(examinationFormId));
            }
        }
        return trainingProgramsModelList;
    }

    /**
     * 导出错误数据
     *
     * @param response 响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 李建帅
     * @since 2018-12-2 08:11:58
     */
    @Override
    public boolean downErrorList(String errorListId, HttpServletResponse response) {
            // redis 取出没有成功导入的培养计划信息
            Set set = redisTemplate.opsForZSet().range(errorListId, 0, -1);
            List<TrainingProgramsTemplate> trainingProgramsTemplateList = new ArrayList<>(set);
            // 清空redis数据
            redisTemplate.delete(errorListId);

        //导出
        if (trainingProgramsTemplateList.size() == 0) {
            return false;
        }
        Map<Serializable, Serializable> errorMap = new HashMap<>(16);
        errorMap.put(NAME, "未导入的培养计划信息");
        errorMap.put(SHEET_NAME, "培养计划");
        errorMap.put(TITLE, "未导入的信息");
        errorMap.put(COLUMNS, new String[]{"semesterName", "professionName", "grade", "courseCode", "courseName", "theoryClassHour", "practiceClassHour", "examinationForm","failReason"});
        errorMap.put(DATA_LIST, (Serializable) trainingProgramsTemplateList);
        try {
            ExcelUtil.exportExcel(errorMap, response);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<TrainingProgramsEntity> findCourseId() {
        //从工具类中获取当前学年的ID
        String semester= YearSemester.getYearSemesterCode(0,0).get(0);

        TrainingProgramsEntity trainingProgramsEntity=new TrainingProgramsEntity();
        trainingProgramsEntity.setSemesterId(semester);
        return trainingProgramsDao.findCourseId(trainingProgramsEntity);
    }


    @Override
    public List<TrainingProgramsModel> findByProfessionId(String professionId) {
        List<TrainingProgramsModel> trainList=new ArrayList<>();
        List<TrainingProgramsEntity> trainingProgramsEntityList= findCourseId();
        List<CourseEntity> courseEntityList=new ArrayList<>();
        //TODO
        for (TrainingProgramsEntity courseId:trainingProgramsEntityList) {
            courseEntityList.add(courseService.getById(courseId.getCourseId()));
        }
        for (CourseEntity courseName:courseEntityList) {
            TrainingProgramsModel traModel=new TrainingProgramsModel();
            traModel.setCourseName(courseName.getName());
            traModel.setCourseCategoryId(courseName.getCourseCategory());
            traModel.setCourseTypeId(courseName.getCourseType());
            traModel.setCourseCode(courseName.getCode());
            traModel.setCourseId(courseName.getId());

            trainList.add(traModel);
        }
        return trainList;
    }

    @Override
    public List<TrainingProgramsModel> findCourseByProfessionId(String professionId) {
        //从工具类中获取当前学年的ID
        String semester= YearSemester.getYearSemesterCode(0,0).get(0);

        return trainingProgramsDao.findCourseByProfessionId(professionId,semester);
    }
}
